<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>图片轮播（过度效果）</title>
    <style>
        #lunbo {
            width: 1226px;
            height: 460px;
            position: relative;
            margin: 0 auto;
        }
        #lunbo img {
            width: 520px;
            height: 520px;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            /*display: none;*/
        }
    </style>
</head>
<body>
<div id="lunbo">
    <img src="img/lunbo1.png">
    <img src="img/lunbo6.png">
    <img src="img/lunbo3.png">
    <img src="img/lunbo4.png">
    <img src="img/lunbo5.png">
    <img src="img/lunbo2.png">
</div>
<script>
    let imgArr = document.getElementById('lunbo').getElementsByTagName('img');
    //图片数量
    let imgNum = imgArr.length;
    //当前图片索引
    let index = 0;
    //上一张图片索引
    let preIndex = index;
    //当前图片初始透明度
    let opacityValue = 0;
    //上一张图片初始透明度
    let preOpacityValue = 1;
    //初始化第一张图片
    imgArr[index].style.opacity = 1;
    setInterval(function() {
        //每次切换时，确定当前图片索引与上一张图片索引
        preIndex = index;
        index++;
        //判断是否进行下一轮轮播
        if (index > imgNum-1) {
            index = 0;
        }
        //上一张图片隐藏
        hideImg();
        //当前图片显示
        showImg();
    }, 3000);
    function showImg() {
        //设置当前显示图片透明度初始值
        opacityValue = 0;
        //淡入动画
        let time = setInterval(function() {
            opacityValue += 0.05;
            if (opacityValue >= 1) {
                opacityValue = 1;
                clearInterval(time);
            }
            imgArr[index].style.opacity = opacityValue;
        }, 40);
    }
    function hideImg() {
        //设置上一张隐藏图片透明度初始值
        preOpacityValue = 1;
        //淡出动画
        let time = setInterval(function() {
            preOpacityValue -= 0.05;
            if (preOpacityValue <= 0) {
                preOpacityValue = 0;
                clearInterval(time);
            }
            imgArr[preIndex].style.opacity = preOpacityValue;
        }, 40);
    }
</script>
</body>
</html>